package util.android.adapters;

import java.util.List;

import com.androidutil.R;

import util.android.beans.Item;
import android.content.Context;
import android.widget.ArrayAdapter;

public abstract class ItemSpinnerAdapter<T> 
extends ArrayAdapter<Item>
{
	
	public ItemSpinnerAdapter(Context context) {
		super(context, R.layout.spinner_layout);
	}
	
	public ItemSpinnerAdapter(Context context, T[] arreglo) {
		this(context);
		
		for(T obj : arreglo) {
			Item it = new Item();
			it.setKey(getKey(obj));
			it.setLabel(getLabel(obj));	
			it.setObjeto(obj);
			this.add(it);
		}	
	}

	@SuppressWarnings("unchecked")
	public ItemSpinnerAdapter(Context context, List<T> lista) {
		this(context, (T[])lista.toArray());		
	}
	
	public ItemSpinnerAdapter<T> agregar(int index, String key, String label) {
		Item it = new Item();
		it.setKey(key);
		it.setLabel(label);
		this.insert(it, index);
		return this;
	}
	
	public int getPositionByKey(String key) {
		Item it = new Item();
		it.setKey(key);
		return this.getPosition(it);
	}
	
	public abstract String getKey(T obj);
	public abstract String getLabel(T obj);

}
